﻿#include "HJFrame.h"

#include "bsp/bsp_crc.h"
#include "bsp/bsp_converter.h"

#define BYTE_ORDER       true

//组包 数据 -> 包
void Frame::make_ease_pack(uint8_t* src_addr, uint8_t* des_addr)
{
    this->head1 = 0xaa;
    this->head2 = 0x55;

    memcpy(&this->src_addr, src_addr, 4);
    memcpy(&this->des_addr, des_addr, 4);

    this->cmd = UPGRADE_EASE_CMD;
    this->paload_len = 0;

    this->end1 = 0x55;
    this->end2 = 0xaa;
}

//组包 数据 -> 包
void Frame::make_reboot_pack(uint8_t* src_addr, uint8_t* des_addr)
{
    this->head1 = 0xaa;
    this->head2 = 0x55;

    memcpy(&this->src_addr, src_addr, 4);
    memcpy(&this->des_addr, des_addr, 4);

    this->cmd = UPGRADE_REBOOT_CMD;
    this->paload_len = 0;

    this->end1 = 0x55;
    this->end2 = 0xaa;
}


void Frame::make_read_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t read_addr, uint16_t read_len)
{
    this->head1 = 0xaa;
    this->head2 = 0x55;

    memcpy(&this->src_addr, src_addr, 4);
    memcpy(&this->des_addr, des_addr, 4);

    this->cmd = UPGRADE_READ_CMD;
    bsp_cnv_uint32_to_arr(this->paload, read_addr, BYTE_ORDER);
    bsp_cnv_uint16_to_arr(this->paload + 4, read_len, BYTE_ORDER);
    this->paload_len = 6;

    this->end1 = 0x55;
    this->end2 = 0xaa;
}

//组包 数据 -> 包
void Frame::make_write_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t write_addr, uint8_t* write_data, uint16_t* write_len)
{
    this->head1 = 0xaa;
    this->head2 = 0x55;

    memcpy(&this->src_addr, src_addr, 4);
    memcpy(&this->des_addr, des_addr, 4);

    this->cmd = UPGRADE_WRITE_CMD;
    bsp_cnv_uint32_to_arr(this->paload, write_addr, BYTE_ORDER);
    memcpy(this->paload + 4, write_data, *write_len);
    this->paload_len = *write_len + 4;

    this->end1 = 0x55;
    this->end2 = 0xaa;
}

void Frame::unmake_write_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t* write_addr, uint8_t** write_data, uint16_t* write_len)
{
    if(src_addr != NULL)
        memcpy(src_addr, &this->src_addr, 4);
    if(des_addr != NULL)
        memcpy(des_addr, &this->des_addr, 4);

    if(write_addr != NULL)
        *write_addr = bsp_cnv_arr_to_uint32(this->paload, BYTE_ORDER);

    if(write_data != NULL)
        *write_data = this->paload + 4;

    if(write_len != NULL)
        *write_len = this->paload_len - 4;
}

void Frame::unmake_read_pack(uint8_t* src_addr, uint8_t* des_addr, uint32_t* read_addr, uint16_t* read_len)
{
    if(src_addr != NULL)
        memcpy(src_addr, &this->src_addr, 4);
    if(des_addr != NULL)
        memcpy(des_addr, &this->des_addr, 4);

    if(read_addr != NULL)
        *read_addr = bsp_cnv_arr_to_uint32(this->paload, BYTE_ORDER);

    if(read_len != NULL)
        *read_len =  bsp_cnv_arr_to_uint16(this->paload + 4, BYTE_ORDER);
}

//组包 包 -> 数组
void Frame::make_pack(uint8_t* buff, uint16_t* len)
{
    uint16_t index = 0;
    uint32_t crc = 0x11223344;
    buff[index++] = 0xAA;
    buff[index++] = 0x55;

    bsp_cnv_uint16_to_arr(buff + index, this->paload_len + 20 - 4, BYTE_ORDER);
    index += 2;

    memcpy(buff + index, this->src_addr, 4);
    index += 4;

    memcpy(buff + index, this->des_addr, 4);
    index += 4;

    bsp_cnv_uint16_to_arr(buff + index, this->cmd, BYTE_ORDER);
    index += 2;

    memcpy(buff + index, this->paload, this->paload_len);
    index += this->paload_len;

    crc = bsp_crc32(buff + 2, index - 2);

    bsp_cnv_uint32_to_arr(buff + index, crc, BYTE_ORDER);
    index += 4;

    buff[index++] = 0x55;
    buff[index++] = 0xAA;
    *len = index;
}

//解包 数组 -> 包
void Frame::unmake_pack(uint8_t* buff, uint16_t* len)
{
    uint16_t index = 0;
    uint32_t crc = 0x11223344;
    this->head1 = buff[index++];
    this->head2 = buff[index++];

    this->len = bsp_cnv_arr_to_uint16(buff + index, BYTE_ORDER);
    index += 2;

    memcpy(this->src_addr, buff + index, 4);
    index += 4;

    memcpy(this->des_addr, buff + index, 4);
    index += 4;

    this->cmd = bsp_cnv_arr_to_uint16(buff + index, BYTE_ORDER);
    index += 2;

    this->paload_len = *len - 20;
    memcpy(this->paload, buff + index, this->paload_len);
    index += this->paload_len;

    crc = bsp_cnv_arr_to_uint32(buff + index, BYTE_ORDER);
    index += 4;

    this->end1 = buff[index++];
    this->end2 = buff[index++];
}

uint16_t rx_len=0;

bool FrameRecv::proc_one_byte(Frame* frame,uint8_t data)
{
    rx_len ++;
    if (recv_len == 0)
    {
        recv_buff[0] = recv_buff[1];
        recv_buff[1] = recv_buff[2];
        recv_buff[2] = recv_buff[3];
        recv_buff[3] = data;

        uint16_t len = bsp_cnv_arr_to_uint16(&recv_buff[2], BYTE_ORDER);
        if (recv_buff[0] == 0xAA && recv_buff[1] == 0x55 &&
            len <= 1034 && len > 10)
        {
            recv_index = 4;
            recv_len = len + 4;
            //qDebug("接收到帧头");
        }
    }
    else
    {
        recv_buff[recv_index++] = data;
        if (recv_index >= recv_len)
        {
            if (recv_buff[recv_index - 2] == 0x55 && recv_buff[recv_index - 1] == 0xAA)
            {
                uint32_t crc = bsp_crc32(recv_buff + 2, recv_len - 8);
                uint32_t pack_crc = bsp_cnv_arr_to_uint32(&recv_buff[recv_index - 6], BYTE_ORDER);//BYTE_ORDER
                if (pack_crc == crc)//crc校验
                {
                    frame->unmake_pack(recv_buff, &recv_len); //解包
                    recv_index = 0;
                    recv_len = 0;
                    return true;
                }
                else
                {
                    //qDebug("crc校验失败");
                }
            }

            recv_index = 0;
            recv_len = 0;
        }
    }
    return false;
}


